package com.chenglu.android.store.mvp.presenter;





import com.chenglu.android.bean.SmsCodeBean;
import com.chenglu.android.common.base.BasePresenter;
import com.chenglu.android.common.net.Constant;
import com.chenglu.android.common.net.RxScheduler;
import com.chenglu.android.common.net.exception.NetErrorException;
import com.chenglu.android.common.utils.LogUtils;
import com.chenglu.android.common.utils.ToastUtils;
import com.chenglu.android.goodsinfo.bean.GoodsCommentBean;
import com.chenglu.android.goodsinfo.bean.GoodsInfoBean;
import com.chenglu.android.goodsinfo.bean.ShopsinfoBean;
import com.chenglu.android.index.bean.IndexRecomBean;
import com.chenglu.android.mine.bean.CommonDataBean;
import com.chenglu.android.store.bean.ChartBangBean;
import com.chenglu.android.store.mvp.contract.StoreContract;
import com.chenglu.android.store.mvp.model.StoreModel;

import java.util.Map;

import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import okhttp3.RequestBody;


/**
 * @author: lyj
 * @Created by Copyright 2023/7/27 Guangzhou Hongma Technology Co., Ltd. All rights reserved.
 *
 */
public class StorePresenter extends BasePresenter<StoreContract.View> implements StoreContract.StorePresenter {

     private  StoreContract.StoreModel model;

    public StorePresenter(){
        model = new StoreModel();
    }


    @Override
    public void getShopinfo(String tenantId) {
        if(!isViewAttached()){
            return;
        }
        model.getShopinfo(tenantId)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<ShopsinfoBean>() {
                    //订阅
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();

                    }

                    @Override
                    public void onNext(@NonNull ShopsinfoBean bean) {

                        if(bean.getCode()==(Constant.HTTPCODE200)){
                            mView.getShopinfo(bean);
                        }else {
                            ToastUtils.show(bean.getMsg());
                        }

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mView.dismissLoading();
                        NetErrorException.apiError(e,mView);
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                        LogUtils.e("qqqqqqq:"+"onComplete");

                    }
                });
    }


    @Override
    public void getShopsTypes(String token,int type,String tenantId) {
        if(!isViewAttached()){
            return;
        }
        model.getShopsTypes(token,type,tenantId)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<ChartBangBean>() {
                    //订阅
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();
//                        LogUtils.e("qqqqqqq:"+"onSubscribe");
                    }

                    @Override
                    public void onNext(@NonNull ChartBangBean bean) {
//                        LogUtils.e("qqqqqqq:"+"onNext");

//                        mView.getUserInfoSuccess(bean);
                        if(bean.getCode()==Constant.HTTPCODE200){
                            mView.getShopsTypes(bean);
                        }else {
                            ToastUtils.show(bean.getMsg());
                        }

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mView.dismissLoading();
                        NetErrorException.apiError(e,mView);
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
//                        LogUtils.e("qqqqqqq:"+"onComplete");

                    }
                });
    }

    @Override
    public void getLikeLists(Map<String, String> map) {
        if(!isViewAttached()){
            return;
        }
        model.getLikeLists(map)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<IndexRecomBean>() {
                    //订阅
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();
//                        LogUtils.e("qqqqqqq:"+"onSubscribe");
                    }

                    @Override
                    public void onNext(@NonNull IndexRecomBean bean) {
//                        LogUtils.e("qqqqqqq:"+"onNext");

//                        mView.getUserInfoSuccess(bean);
                        if(bean.getCode()==Constant.HTTPCODE200){
                            mView.getLikeLists(bean);
                        }else {
                            ToastUtils.show(bean.getMsg());
                        }

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mView.dismissLoading();
                        NetErrorException.apiError(e,mView);
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
//                        LogUtils.e("qqqqqqq:"+"onComplete");

                    }
                });
    }

    @Override
    public void getSearchGoods(Map<String, String> map) {
        if(!isViewAttached()){
            return;
        }
        model.getSearchGoods(map)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<IndexRecomBean>() {
                    //订阅
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();
//                        LogUtils.e("qqqqqqq:"+"onSubscribe");
                    }

                    @Override
                    public void onNext(@NonNull IndexRecomBean bean) {
//                        LogUtils.e("qqqqqqq:"+"onNext");

//                        mView.getUserInfoSuccess(bean);
                        if(bean.getCode()==Constant.HTTPCODE200){
                            mView.getSearchGoods(bean);
                        }else {
                            ToastUtils.show(bean.getMsg());
                        }

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mView.dismissLoading();
                        NetErrorException.apiError(e,mView);
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
//                        LogUtils.e("qqqqqqq:"+"onComplete");

                    }
                });
    }

    @Override
    public void getShopsRecommendPage(String tenantId,Map<String, String> map) {
        if(!isViewAttached()){
            return;
        }
        model.getShopsRecommendPage(tenantId,map)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<IndexRecomBean>() {
                    //订阅
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();
//                        LogUtils.e("qqqqqqq:"+"onSubscribe");
                    }

                    @Override
                    public void onNext(@NonNull IndexRecomBean bean) {
//                        LogUtils.e("qqqqqqq:"+"onNext");

//                        mView.getUserInfoSuccess(bean);
                        if(bean.getCode()==Constant.HTTPCODE200){
                            mView.getShopsRecommendPage(bean);
                        }else {
                            ToastUtils.show(bean.getMsg());
                        }

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mView.dismissLoading();
                        NetErrorException.apiError(e,mView);
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
//                        LogUtils.e("qqqqqqq:"+"onComplete");

                    }
                });
    }


}
